package com.example.newland.movedame;

import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "调试";
    private ImageView iv1;
    private AnimationDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

//        animation = (AnimationDrawable) iv1.getBackground();

        animation = (AnimationDrawable)getResources().getDrawable(R.drawable.start_show);
        iv1.setBackgroundDrawable(animation);
    }

    private void executeAlpha(View view){
        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
        alphaAnimation.setDuration(3000);       //动画持续时间
        animation.setFillAfter(true);           //保持动画结束的状态
        view.startAnimation(alphaAnimation);
    }

    private void executeRotate(View view){
        RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(3000);
        view.startAnimation(rotateAnimation);
    }

    private void executeScale(View view){
        ScaleAnimation scaleAnimation = new ScaleAnimation(0,2f,0,2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scaleAnimation.setDuration(3000);
        view.startAnimation(scaleAnimation);
    }

    private void executeTranslate(View view){

        ViewGroup viewGroup = (ViewGroup) view.getParent();

        int gropHeight = viewGroup.getHeight();
        int gropWidth = viewGroup.getWidth();
        int viewHeight = view.getHeight();
        int viewWidth = view.getWidth();


        int top = view.getTop();
        int left = view.getLeft();
        gropHeight = (gropHeight-viewHeight)/2;
        gropWidth = (gropWidth-viewWidth)/2;


        Log.d(TAG, "gropHeight: "+gropHeight+"left: "+gropWidth+"viewHeight: "+viewHeight+"viewWidth: "+viewWidth);

        TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,left,Animation.ABSOLUTE,gropWidth-left,Animation.ABSOLUTE,top,Animation.ABSOLUTE,gropHeight-top);
        animation.setDuration(3000);
        view.startAnimation(animation);
    }

    public void click(View view) {
        switch (view.getId()){
            case R.id.bt1:
                executeAlpha(iv1);
                break;
            case R.id.bt2:
                executeRotate(iv1);
                break;
            case R.id.bt3:
                executeScale(iv1);
                break;
            case R.id.bt4:
                executeTranslate(iv1);
                break;
            case R.id.bt5:
                animation.start();;
                break;
        }
    }

    private void initView(){
        iv1 = findViewById(R.id.iv1);
    }
}
